#include <iostream>
#include <unistd.h>
#include <time.h>
#include "BlockQueue.hpp"
#include "Task.hpp"

std::string opers = "+-*/%($_@)";

void* Productor(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    //生产任务
    while (true)
    {
        int date1 = rand() % 10;
        usleep(123);
        int date2 = rand() % 10;
        usleep(123);
        char op = opers[rand() % opers.size()];
        Task t(date1, op, date2);
        bq->Push(t);
        t.PrintQues();
        sleep(1);
    }
}

void* Consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while (true)
    {
        Task t;
        bq->Pop(&t);
        t.Run();
        t.PrintRest();
    }   
}

int main()
{
    srand((unsigned)time(nullptr));
    BlockQueue<Task> bq(5);
    pthread_t p, c;
    pthread_create(&p, nullptr, Productor, (void *)&bq); 
    pthread_create(&c, nullptr, Consumer, (void *)&bq); 

    pthread_join(p, nullptr);
    pthread_join(c, nullptr);

    return 0;
}